package com.LC.Controller.BKManagement;

import com.LC.Constant.MessageConstant;
import com.LC.Constant.RedisConstant;
import com.LC.Entity.*;
import com.LC.Service.SetMealService;
import com.LC.Utils.FileUploadAndDeleteUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
public class CheckMealController {

    @Reference
    private SetMealService setMealService;

    @Autowired
    private JedisPool jedisPool;
    /**
     * 查找所有检查组
     * @return
     */
    @RequestMapping("/BKManagement/SetMeal/CheckGroupList")
    public List<CheckGroup> findAllCheckGroup(){
        return setMealService.findAllCheckGroup();
    }
    
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/BKManagement/SetMeal/MealList")
    public Result showMeal(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try{
            pageResult = setMealService.showMeal(queryPageBean);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,pageResult);
    }

    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/BKManagement/SetMeal/Upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        System.out.println(imgFile);
        String OriginalFileName = imgFile.getOriginalFilename();
        int i = OriginalFileName.lastIndexOf(".");
        String extention = OriginalFileName.substring(i - 1);//获得后缀 example：.jpg
        String fileName = UUID.randomUUID().toString() + extention;
        try {
            //将文件上传到七牛服务器
            FileUploadAndDeleteUtils.uploadToQiniu(imgFile.getBytes(),fileName);
            //将上传的图片名存到redis中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }
    
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/BKManagement/SetMeal/NewMeal")
    public Result addMeal(@RequestBody SetMeal setMeal,Integer[] checkGroupIds){
        try{
            setMealService.addMeal(setMeal,checkGroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }
    
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/BKManagement/SetMeal/MealById")
    public Result findMealById(int mealId){
        SetMeal setMeal = null;
        try {
            setMeal = setMealService.findById(mealId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setMeal);
    }
    
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/BKManagement/SetMeal/CheckGroupIds")
    public Result findCheckGroupIdsByMealId(int mealId){
        List<Integer> checkGroupIdsByMealId = null;
        try {
            checkGroupIdsByMealId = setMealService.findCheckGroupIdsByMealId(mealId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupIdsByMealId);
    }

  /**
   * 更新套餐信息
   * @param checkGroupIds
   * @param setMeal
   * @return
   */
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    @RequestMapping("/BKManagement/SetMeal/UD")
    public Result updateMealData(Integer[] checkGroupIds,@RequestBody SetMeal setMeal){
        try {
            setMealService.updateMealData(checkGroupIds,setMeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

  /**
   * 删除套餐
   * @param MealId
   * @return
   */
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    @RequestMapping("/BKManagement/SetMeal/DE")
    public Result deleteMeal(int MealId){
        try {
          setMealService.deleteMeal(MealId);
        } catch (Exception e) {
          e.printStackTrace();
          return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}

